<?php

use Illuminate\Http\Request;
use Overtrue\Pinyin\Pinyin;
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ArticleController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\TagController;
use App\Http\Controllers\MessageController;
use App\Http\Controllers\PhotoController;
use App\Http\Controllers\StatsController;
use App\Http\Controllers\UploadController;
use App\Http\Controllers\AvatarController;

// RESTful 规范
// GET：用于获取资源（只读操作），应为幂等。
// POST：用于创建资源或触发非幂等操作。
// PUT：用于完整更新资源（替换整个资源）。
// PATCH：用于部分更新资源。
// DELETE：用于删除资源。

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
| 公开接口（无需认证）
*/

// 测试接口
Route::get('/test', function(){
    return response()->json([
        'data' => [
            'name' => '四季豆',
            'url' => 'https://blog.csdn.net/php_ACDE?type=blog'
        ],
        'message' => 'hello world'
    ], 200);
});

// 测试slug生成接口
Route::get('/test-slug', function(Request $request) {
    $text = $request->input('text');

    $pinyin = new Pinyin();

    $slug = $pinyin->permalink($text);

    return response()->json([
        'original_title' => $text,
        'generated_slug' => $slug,
    ]);
});

// 用户登录
Route::post('login', [AuthController::class, 'login']);

// 分类、标签、文章公开接口
Route::get('/latest-categories', [CategoryController::class, 'latest']);
Route::get('/latest-tag', [TagController::class, 'latest']);

// 前台留言
Route::get('/messages/publicIndex', [MessageController::class, 'publicIndex']);
Route::post('/messages/publicStore', [MessageController::class, 'publicStore']);

// 相册公开接口
Route::get('/photos/publicIndex', [PhotoController::class, 'publicIndex']);

/*
|--------------------------------------------------------------------------
| 受保护接口（需要认证）
|--------------------------------------------------------------------------
*/
Route::middleware('auth:sanctum')->group(function () {

    // 测试token接口
    Route::post('/test', function(){
        return response()->json([
            'data' => [
                'name' => '四季豆',
                'url' => 'https://blog.csdn.net/php_ACDE?type=blog'
            ],
            'message' => 'hello world'
        ], 200);
    });

    // 用户相关
    Route::prefix('auth')->group(function(){
        Route::get('/user', [AuthController::class, 'getUserInfo']);
        Route::delete('/logout', [AuthController::class, 'logout']);
        Route::patch('/nickname', [AuthController::class, 'updateNickname']);
    });
    
    // 分类管理
    Route::apiResource('categories', CategoryController::class);

    // 标签管理
    Route::apiResource('tags', TagController::class);

    // 文章管理
    Route::apiResource('articles', ArticleController::class);
    Route::post('articles/batch-destroy', [ArticleController::class, 'destroyBatch']);

    // 后台留言
    Route::middleware('auth:sanctum')->group(function () {
        Route::get('messages', [MessageController::class, 'index']); // 留言列表
        Route::patch('messages/{message}/status', [MessageController::class, 'updateStatus']); // 审核
        Route::post('messages/{message}/reply', [MessageController::class, 'reply']); // 回复
        Route::delete('messages/{message}', [MessageController::class, 'destroy']); // 删除
        Route::delete('messages', [MessageController::class, 'destroyBatch']); // 批量删除
    });

    // 相册管理
    Route::prefix('photos')->group(function () {
        Route::get('/', [PhotoController::class, 'adminIndex']);

        Route::post('/show', [UploadController::class, 'show']);
        Route::post('/store', [UploadController::class, 'store']);
        Route::post('/update', [UploadController::class, 'update']);
        Route::post('/destroy', [UploadController::class, 'destroy']);
    });

    // 统计相关
    Route::prefix('stats')->group(function () {
        Route::get('dashboard', [StatsController::class, 'dashboard']);
        Route::get('articles', [StatsController::class, 'articleStats']);
        Route::get('categories', [StatsController::class, 'categoryStats']);
        Route::get('tags', [StatsController::class, 'tagStats']);
        Route::get('messages', [StatsController::class, 'messageStats']);
        Route::get('users', [StatsController::class, 'userStats']);
        Route::get('photos', [StatsController::class, 'photoStats']);
        Route::get('comments', [StatsController::class, 'commentStats']);
    });

    // 文件上传
    Route::prefix('upload')->group(function () {
        // 头像上传
        Route::post('/avatar', [UploadController::class, 'uploadAvatar']);
        // 封面上传
        Route::patch('/cover', [UploadController::class, 'uploadCover']);
        // 图片上传
        Route::patch('/photo', [UploadController::class, 'uploadPhoto']);
    });
});